package com.rt.easyjava.base.biz1.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.easyjava.base.biz1.bean.TmptUserSetting;
import com.rt.easyjava.base.biz1.dto.TmptUserSettingDto;
import com.rt.easyjava.base.biz1.service.intf.TmptUserSettingService;
import com.rt.easyjava.common.base.MyQueryWrapper;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.base.UserDetailsDto;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.exception.ControllerException;
import com.rt.easyjava.common.utils.others.DateTool;
import com.rt.easyjava.common.utils.others.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 前端控制器
 * @author wuxiaoyong
 * @since 2020-03-18
 * 用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，_userJson)
 */
@Api(tags = "TmptUserSettingController - 控制类")
@RequestMapping("/coding/tmptUserSetting")
@Controller
public class TmptUserSettingController extends ControllerException {
    private final Logger logger = LoggerFactory.getLogger(TmptUserSettingController.class);

    @Autowired
    private MyMapper mapper;
    @Autowired
    private TmptUserSettingService tmptUserSettingService;


    // @PreAuthorize("hasAuthority('tmptUserSetting:create')") 所有页面都要访问这个功能，权限就关了
    @ApiOperation("新增对象")
    @PostMapping(value = "/createOrUpdate4GlobalUnique")
    @ResponseBody
    public Result createOrUpdate4GlobalUnique(@RequestBody TmptUserSettingDto tmptUserSettingDto,
                                 String _userId, String _userName, String _companyId){
        if (StringUtils.isNull(_userId)) _userId="0";
        if (StringUtils.isNull(_companyId)) _companyId="0";
        if (StringUtils.isNull(_userName)) _userName="system";
        // 插入参数
        // create
        tmptUserSettingDto.setCreatemanId(Long.parseLong(_userId));
        tmptUserSettingDto.setCreateman(_userName);
        tmptUserSettingDto.setCreatedate(DateTool.getCurrentDatetimeStr());
        tmptUserSettingDto.setCompanyId(Long.parseLong(_companyId));
        // update
        tmptUserSettingDto.setModifymanId(Long.parseLong(_userId));
        tmptUserSettingDto.setModifyman(_userName);
        tmptUserSettingDto.setModifydate(DateTool.getCurrentDatetimeStr());

        //调用服务
        return tmptUserSettingService.createOrUpdateGlobalUnique(tmptUserSettingDto);
    }

    // @PreAuthorize("hasAuthority('tmptUserSetting:query')") 所有页面都要访问这个功能，权限就关了
    @ApiOperation("自定义sql分页查询")
    @GetMapping(value = "/myPage4GlobalUnique")
    @ResponseBody
    public Result myPage4GlobalUnique(@ModelAttribute TmptUserSettingDto tmptUserSettingDto){

        Page<TmptUserSetting> page = new Page<>(tmptUserSettingDto.getPage(), tmptUserSettingDto.getLimit());

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<TmptUserSetting> queryWrapper = MyQueryWrapper.buildQueryWrapper(new TmptUserSetting(), "name");
        queryWrapper.eq(tmptUserSettingDto.getName()!=null,"t1.name",tmptUserSettingDto.getName());
        queryWrapper.eq(true,"t1.deleteflag",0);
        queryWrapper.orderBy(tmptUserSettingDto.getCondition(), tmptUserSettingDto.isAsc(), tmptUserSettingDto.getColumn());

        return tmptUserSettingService.myPage(page, queryWrapper);
    }

    // @PreAuthorize("hasAuthority('tmptUserSetting:query')") 所有页面都要访问这个功能，权限就关了
    @ApiOperation("自定义sql分页查询")
    @GetMapping(value = "/myPage4GlobalUniqueByName")
    @ResponseBody
    public Result myPage4GlobalUniqueByName(String name){
        Page<TmptUserSetting> page = new Page<>(1, 10);
        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<TmptUserSetting> queryWrapper = MyQueryWrapper.buildQueryWrapper(new TmptUserSetting(), "name");
        queryWrapper.eq("t1.name",name);
        queryWrapper.eq(true,"t1.deleteflag",0);
        Result<TmptUserSettingDto> tmptUserSettingDtoResult = tmptUserSettingService.myPage(page, queryWrapper);
        List<TmptUserSettingDto> userSettingDtoList = (List<TmptUserSettingDto>)tmptUserSettingDtoResult.getMap().get("rows");
        if(userSettingDtoList.isEmpty()){
            return Result.builder("");
        }
        return Result.builder(userSettingDtoList.get(0).getValue());
    }

    // @PreAuthorize("hasAuthority('tmptUserSetting:query')") 所有页面都要访问这个功能，权限就关了
    @ApiOperation("自定义sql分页查询")
    @GetMapping(value = "/myPage4GlobalUniqueLike")
    @ResponseBody
    public Result myPage4GlobalUniqueLike(@ModelAttribute TmptUserSettingDto tmptUserSettingDto){

        Page<TmptUserSetting> page = new Page<>(tmptUserSettingDto.getPage(), tmptUserSettingDto.getLimit());

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<TmptUserSetting> queryWrapper = MyQueryWrapper.buildQueryWrapper(new TmptUserSetting(), "name");
        queryWrapper.like(tmptUserSettingDto.getName()!=null,"t1.name",tmptUserSettingDto.getName());
        queryWrapper.eq(true,"t1.deleteflag",0);
        queryWrapper.orderBy(tmptUserSettingDto.getCondition(), tmptUserSettingDto.isAsc(), tmptUserSettingDto.getColumn());

        return tmptUserSettingService.myPage(page, queryWrapper);
    }


    // @PreAuthorize("hasAuthority('tmptUserSetting:create')") 所有页面都要访问这个功能，权限就关了
    @ApiOperation("新增对象")
    @PostMapping(value = "/createOrUpdate")
    @ResponseBody
    public Result createOrUpdate(@RequestBody TmptUserSettingDto tmptUserSettingDto,
                                 String _userId, String _userName, String _companyId){
        // 插入参数
        // create
        tmptUserSettingDto.setCreatemanId(Long.parseLong(_userId));
        tmptUserSettingDto.setCreateman(_userName);
        tmptUserSettingDto.setCreatedate(DateTool.getCurrentDatetimeStr());
        tmptUserSettingDto.setCompanyId(Long.parseLong(_companyId));
        // update
        tmptUserSettingDto.setModifymanId(Long.parseLong(_userId));
        tmptUserSettingDto.setModifyman(_userName);
        tmptUserSettingDto.setModifydate(DateTool.getCurrentDatetimeStr());

        //调用服务
        return tmptUserSettingService.createOrUpdate(tmptUserSettingDto);
    }

    // @PreAuthorize("hasAuthority('tmptUserSetting:query')") 所有页面都要访问这个功能，权限就关了
    @ApiOperation("自定义sql分页查询")
    @GetMapping(value = "/myPage")
    @ResponseBody
    public Result myPage(@ModelAttribute TmptUserSettingDto tmptUserSettingDto){
        Result<TmptUserSettingDto> result = new Result<>();

        Page<TmptUserSetting> page = new Page<>(tmptUserSettingDto.getPage(), tmptUserSettingDto.getLimit());

        TmptUserSetting tmptUserSetting = mapper.map(tmptUserSettingDto, TmptUserSetting.class);
        if(tmptUserSetting.getDeleteflag()==null){
            tmptUserSetting.setDeleteflag(0);
        }

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<TmptUserSetting> queryWrapper = MyQueryWrapper.buildQueryWrapper(tmptUserSetting, "createdate", "createman", "name");
        //特殊查询条件
        queryWrapper.gt(tmptUserSettingDto.getCreatedate_Start()!=null,"t1.createdate",tmptUserSettingDto.getCreatedate_Start());
        queryWrapper.lt(tmptUserSettingDto.getCreatedate_End()!=null,"t1.createdate",tmptUserSettingDto.getCreatedate_End());
        queryWrapper.like(tmptUserSettingDto.getCreateman()!=null,"t1.createman",tmptUserSettingDto.getCreateman());
        queryWrapper.like(tmptUserSettingDto.getName()!=null,"t1.name",tmptUserSettingDto.getName());
        queryWrapper.orderBy(tmptUserSettingDto.getCondition(), tmptUserSettingDto.isAsc(), tmptUserSettingDto.getColumn());


        result = tmptUserSettingService.myPage(page, queryWrapper);

        return result;
    }

    @ApiOperation("自定义sql分页查询")
    @GetMapping(value = "/myPage4CurrentUser")
    @ResponseBody
    public Result myPage4CurrentUser(@ModelAttribute TmptUserSettingDto tmptUserSettingDto, String userDetailsJson){
        UserDetailsDto userDetailsDto = UserDetailsDto.getUserDetailsDto(userDetailsJson);
        Result<TmptUserSettingDto> result = new Result<>();

        Page<TmptUserSetting> page = new Page<>(tmptUserSettingDto.getPage(), tmptUserSettingDto.getLimit());

        TmptUserSetting tmptUserSetting = mapper.map(tmptUserSettingDto, TmptUserSetting.class);
        if(tmptUserSetting.getDeleteflag()==null){
            tmptUserSetting.setDeleteflag(0);
        }
        tmptUserSetting.setCreatemanId(Long.parseLong(userDetailsDto.getUserId()));

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<TmptUserSetting> queryWrapper = MyQueryWrapper.buildQueryWrapper(tmptUserSetting, "createdate", "createman", "name");
        //特殊查询条件
        queryWrapper.gt(tmptUserSettingDto.getCreatedate_Start()!=null,"t1.createdate",tmptUserSettingDto.getCreatedate_Start());
        queryWrapper.lt(tmptUserSettingDto.getCreatedate_End()!=null,"t1.createdate",tmptUserSettingDto.getCreatedate_End());
        queryWrapper.like(tmptUserSettingDto.getCreateman()!=null,"t1.createman",tmptUserSettingDto.getCreateman());
        queryWrapper.eq(tmptUserSettingDto.getName()!=null,"t1.name",tmptUserSettingDto.getName());
        queryWrapper.orderBy(tmptUserSettingDto.getCondition(), tmptUserSettingDto.isAsc(), tmptUserSettingDto.getColumn());


        result = tmptUserSettingService.myPage(page, queryWrapper);

        return result;
    }

    // @PreAuthorize("hasAuthority('tmptUserSetting:query')") 所有页面都要访问这个功能，权限就关了
    @ApiOperation("分页查询列表")
    @GetMapping(value = "/page")
    @ResponseBody
    public Result page(@ModelAttribute TmptUserSettingDto tmptUserSettingDto){
        Result<TmptUserSettingDto> result = new Result<>();

        Page<TmptUserSetting> page = new Page<>(tmptUserSettingDto.getPage(), tmptUserSettingDto.getLimit());

        TmptUserSetting tmptUserSetting = mapper.map(tmptUserSettingDto, TmptUserSetting.class);
        if(tmptUserSetting.getDeleteflag()==null){
            tmptUserSetting.setDeleteflag(0);
        }

        QueryWrapper<TmptUserSetting> queryWrapper=new QueryWrapper<>(tmptUserSetting);
        //特殊查询条件
        queryWrapper.gt(tmptUserSettingDto.getCreatedate_Start()!=null,"createdate",tmptUserSettingDto.getCreatedate_Start());
        queryWrapper.lt(tmptUserSettingDto.getCreatedate_End()!=null,"createdate",tmptUserSettingDto.getCreatedate_End());
        tmptUserSetting.setCreateman(null);
        queryWrapper.like(tmptUserSettingDto.getCreateman()!=null,"createman",tmptUserSettingDto.getCreateman());
        queryWrapper.orderBy(tmptUserSettingDto.getCondition(), tmptUserSettingDto.isAsc(), tmptUserSettingDto.getColumn());

        return tmptUserSettingService.page(page, queryWrapper);
    }




    //@PreAuthorize("hasAuthority('tmptUserSetting:query')")
    @ApiOperation("查询所有")
    @GetMapping(value = "/selectAll")
    @ResponseBody
    public Result selectAll(@ModelAttribute TmptUserSettingDto tmptUserSettingDto){
        Result<TmptUserSettingDto> result = new Result<>();


        TmptUserSetting tmptUserSetting = mapper.map(tmptUserSettingDto, TmptUserSetting.class);
        if(tmptUserSetting.getDeleteflag()==null){
            tmptUserSetting.setDeleteflag(0);
        }

        QueryWrapper<TmptUserSetting> queryWrapper=new QueryWrapper<>(tmptUserSetting);
        //特殊查询条件
        queryWrapper.gt(tmptUserSettingDto.getCreatedate_Start()!=null,"createdate",tmptUserSettingDto.getCreatedate_Start());
        queryWrapper.lt(tmptUserSettingDto.getCreatedate_End()!=null,"createdate",tmptUserSettingDto.getCreatedate_End());
        tmptUserSetting.setCreateman(null);
        queryWrapper.like(tmptUserSettingDto.getCreateman()!=null,"createman",tmptUserSettingDto.getCreateman());
        queryWrapper.orderBy(tmptUserSettingDto.getCondition(), tmptUserSettingDto.isAsc(), tmptUserSettingDto.getColumn());

        result = tmptUserSettingService.selectAll(queryWrapper);

        return result;
    }


    //@PreAuthorize("hasAuthority('tmptUserSetting:query')")
    @ApiOperation("selectById")
    @GetMapping(value = "/selectById")
    @ResponseBody
    public Result selectById(Long id){
        //调用服务
        return tmptUserSettingService.selectById(id);
    }



    //@PreAuthorize("hasAuthority('tmptUserSetting:create')")
    @ApiOperation("新增对象")
    @PostMapping(value = "/create")
    @ResponseBody
    public Result create(@RequestBody TmptUserSettingDto tmptUserSettingDto,
                    String _userId, String _userName, String _companyId){
        //插入参数
        tmptUserSettingDto.setCreatemanId(Long.parseLong(_userId));
        tmptUserSettingDto.setCreateman(_userName);
        tmptUserSettingDto.setCreatedate(DateTool.getCurrentDatetimeStr());
        tmptUserSettingDto.setCompanyId(Long.parseLong(_companyId));

        //调用服务
        return tmptUserSettingService.create(tmptUserSettingDto);
    }


    //@PreAuthorize("hasAuthority('tmptUserSetting:update')")
    @ApiOperation("编辑对象")
    @PostMapping(value = "/update")
    @ResponseBody
    public Result update(@RequestBody TmptUserSettingDto tmptUserSettingDto,
                        String _userId, String _userName){
        //参数
        tmptUserSettingDto.setModifymanId(Long.parseLong(_userId));
        tmptUserSettingDto.setModifyman(_userName);
        tmptUserSettingDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptUserSettingDto.setCreatedate(null);
        tmptUserSettingDto.setCreateman(null);
        tmptUserSettingDto.setCreatemanId(null);
        tmptUserSettingDto.setCompanyId(null);

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", tmptUserSettingDto.getId()==null?"0":tmptUserSettingDto.getId().longValue());

        //调用服务
        return tmptUserSettingService.update(tmptUserSettingDto, updateWrapper);
    }


    //@PreAuthorize("hasAuthority('tmptUserSetting:delete')")
    @ApiOperation("逻辑删除对象，由deleteflag控制：0:有效，1:已删除。")
    @PostMapping(value = "/delete")
    @ResponseBody
    public Result delete(Long id, String _userId, String _userName){
        Result result = new Result();

        //参数
        TmptUserSettingDto tmptUserSettingDto = new TmptUserSettingDto();
        tmptUserSettingDto.setModifymanId(Long.parseLong(_userId));
        tmptUserSettingDto.setModifyman(_userName);
        tmptUserSettingDto.setDeleteflag("1");

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id==null?0:id.longValue());

        //调用服务
        return tmptUserSettingService.update(tmptUserSettingDto, updateWrapper);
    }


    //@PreAuthorize("hasAuthority('tmptUserSetting:deletePhysical')")
    @ApiOperation("物理删除对象，默认注释了核心调用方法，请按需启用")
    @PostMapping(value = "/deletePhysical")
    @ResponseBody
    public Result deletePhysical(Long id){
        Result result = new Result();
        if (id==null || id==0) {
            return result.fault("id=null");
        }
        //调用服务
        Boolean aBoolean =  tmptUserSettingService.removeById(id.longValue());
        if(aBoolean){
            result.success();
        }else {
            result.fault();
        }

        return result;
    }

    @ApiOperation("物理删除对象，删除过期的 slice verify uuid, minute=过期分钟 e.g:minute=5")
    @GetMapping(value = "/deletePhysicalBatch4slideVerify")
    @ResponseBody
    public Result deletePhysicalBatch4slideVerify(String minute){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name", "slide-verify-uuid-");
        queryWrapper.lt("createdate", DateTool.getCurrentDate_minute(Integer.parseInt(minute)*-1));
        boolean b = tmptUserSettingService.remove(queryWrapper);
        return Result.builder(b);
    }

}
